package com.example.sixtrainingdemo.view;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.sixtrainingdemo.R;
import com.example.sixtrainingdemo.adapter.GoodsListAdapter;
import com.example.sixtrainingdemo.base.BaseFragment;
import com.example.sixtrainingdemo.contract.MainContract;
import com.example.sixtrainingdemo.entity.Goods;
import com.example.sixtrainingdemo.presenter.MainPresenter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends BaseFragment implements MainContract.IMainView, View.OnClickListener, MainContract.OnItemClickListener  {

   private RecyclerView mGoodsRecyclerView;
   private EditText mSearchEdit;
   private GoodsListAdapter mRecyclerAdapter;
   private MainContract.IMainPresenter mMainPresenter;
   private List<Goods> mGoodsList;

   private Button mGosale;

   @Override
   protected int getLayoutId() {
      return R.layout.fragment_home;
   }

   @Override
   protected void initView() {
      mMainPresenter = new MainPresenter(this, getContext());
      mGoodsList = new ArrayList<>();
      mSearchEdit = mView.findViewById(R.id.search_edit);
      mGoodsRecyclerView = mView.findViewById(R.id.rv_goods);
      mGosale = mView.findViewById(R.id.gosale);
      StaggeredGridLayoutManager staggeredGridLayoutManager =
              new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
      mGoodsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
      mRecyclerAdapter = new GoodsListAdapter(mGoodsList, (MainContract.OnItemClickListener) this);
      mGoodsRecyclerView.setAdapter(mRecyclerAdapter);
      mMainPresenter.getGoodsList();
      mRecyclerAdapter.setOnItemClickListener(this); // 设置监听器
   }

   @Override
   protected void initListener() {
      mGosale.setOnClickListener(this);
   }

   @Override
   public void onResume() {
      super.onResume();
      mMainPresenter.getGoodsList();
   }

   @Override
   public void showGoodsList(List<Goods> goodsList) {
      mGoodsList = goodsList;
      Log.d("", "showGoodsList " + mGoodsList.size());
      mRecyclerAdapter.updateData(mGoodsList);
   }

   @Override
   public void onClick(View v) {
      int id = v.getId();
      if (id == R.id.gosale) {
         Intent intent = new Intent(mContext, Main1Activity.class);
         startActivity(intent);
      }
   }
   public void onItemClick(int position) {
      // 可以在这里根据position获取到点击的Goods对象，并进行操作
      Goods clickedGoods = mGoodsList.get(position);
      Intent intent = new Intent(mContext, ProductDetailsActivity.class);
      intent.putExtra("GOODS_ID", clickedGoods.getId());
      startActivity(intent);
      // 处理点击事件，比如显示Toast或者启动一个新的Activity
      Toast.makeText(getContext(), "Clicked on position: " + clickedGoods.getId(), Toast.LENGTH_SHORT).show();
   }

}




















